﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace HighDivePrint.Model
{
    /// <summary>
    /// List holding dives, used for queue and history).
    /// </summary>
    [Serializable()]
    public class DiveList : IDiveList
    {
        private BindingList<Dive>_list;

        #region Properties
        public BindingList<Dive> List
        { 
            get { return _list;}
            set { _list=value;}
        } 
	    #endregion

        #region Constructors
        public DiveList()
        {
            List = new BindingList<Dive>();
        }

        #endregion

        #region Methods

        #endregion

        #region IDiveList Members

        void IDiveList.Add(Dive D)
        {
            _list.Add(D);
        }

        void IDiveList.Remove(int index)
        {
            _list.RemoveAt(index);
        }

        void IDiveList.SaveToFile()
        {
            Stream stream = new FileStream("Divers.xml", FileMode.Create, FileAccess.Write, FileShare.Write);
            XmlSerializer xs = new XmlSerializer(typeof(DiveList));
            xs.Serialize(stream, this);
            stream.Close();
        }

        void IDiveList.ReadFromFile()
        {
            Stream stream = new FileStream("Divers.xml", FileMode.Create, FileAccess.Read, FileShare.Read);
            XmlSerializer xs = new XmlSerializer(typeof(DiveList));
            DiveList dt = (DiveList)xs.Deserialize(stream);

            _list.Clear();
            foreach (var d in dt.List)
            {
                _list.Add(d);
            }
            stream.Close();
        }

        #endregion
    }
}
